package com.spzx.order.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.common.security.annotation.RequiresLogin;
import com.spzx.order.domain.OrderForm;
import com.spzx.order.domain.OrderInfo;
import com.spzx.order.domain.vo.TradeVo;
import com.spzx.order.service.IOrderInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author xiongkunYang
 * @since 2025/06/05 09:52
 */
@Tag(name = "订单管理")
@RestController
@RequestMapping("/orderInfo")
@RequiredArgsConstructor
public class OrderInfoController extends BaseController {

    private final IOrderInfoService orderInfoService;

    @Operation(summary = "订单结算")
    @RequiresLogin
    @GetMapping("/trade")
    public AjaxResult orderTradeData() {
        TradeVo tradeVo = orderInfoService.orderTradeData();
        return success(tradeVo);
    }

    @Operation(summary = "用户提交订单")
    @RequiresLogin
    @PostMapping("/submitOrder")
    public AjaxResult submitOrder(@RequestBody OrderForm orderForm) {
        return success(orderInfoService.submitOrder(orderForm));
    }

    @Operation(summary = "获取订单信息")
    @RequiresLogin
    @GetMapping("getOrderInfo/{orderId}")
    public AjaxResult getOrderInfo(@PathVariable Long orderId) {
        OrderInfo orderInfo = orderInfoService.getOrderInfoById(orderId);
        return success(orderInfo);
    }

    @Operation(summary = "立即购买")
    @GetMapping("/buy/{skuId}")
    public AjaxResult buy(@PathVariable Long skuId) {
        return success(orderInfoService.buy(skuId));
    }

    @Operation(summary = "获取用户订单分页列表")
    @Parameters({
            @Parameter(name = "pageNum", description = "当前页码", required = true),
            @Parameter(name = "pageSize", description = "每页记录数", required = true),
            @Parameter(name = "orderStatus", description = "订单状态", required = false)
    })
    @GetMapping("/userOrderInfoList/{pageNum}/{pageSize}")
    public TableDataInfo list(@PathVariable Integer pageNum,
                              @PathVariable Integer pageSize,
                              @RequestParam(required = false) Integer orderStatus) {
        try (Page<Object> ignored = PageHelper.startPage(pageNum, pageSize)) {
            List<OrderInfo> list = orderInfoService.getUserOrderInfoList(orderStatus);
            return getDataTable(list);
        }
    }

    @Operation(summary = "取消订单")
    @GetMapping("/cancelOrder/{orderId}")
    public AjaxResult cancelOrder(@PathVariable Long orderId) {
        OrderInfo orderInfo = this.orderInfoService.getById(orderId);
        orderInfoService.cancelOrder(orderInfo, "用户取消订单");
        return success();
    }

    @Operation(summary = "根据订单号获取订单信息")
    @Parameters({
            @Parameter(name = "orderNo", description = "订单号", required = true)
    })
    @InnerAuth
    @GetMapping("getByOrderNo/{orderNo}")
    public R<OrderInfo> getByOrderNo(@PathVariable String orderNo) {
        OrderInfo orderInfo = orderInfoService.getByOrderNo(orderNo);
        return R.ok(orderInfo);
    }


}
